<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false"%>
<jsp:include page="frame/header.jsp"></jsp:include>

<title>新增菜品信息</title>
</head>

<body>
	<div class="container-fluid">
		<div class="row-fluid">
			<div>
				<ul class="breadcrumb">
					<li><a href="#">商家管理</a> <span class="divider">/</span></li>
					<li><a href="#">菜品管理</a></li>
				</ul>
			</div>

			<div class="box">
				<div class="box-header well" data-original-title>
					<h2>
						<i class="icon-edit"></i> 编辑菜品信息
					</h2>
					<div class="box-icon">
						<a href="#" class="btn btn-setting btn-round"><i
							class="icon-cog"></i></a> <a href="#"
							class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
						<a href="#" class="btn btn-close btn-round"><i
							class="icon-remove"></i></a>
					</div>
				</div>
				<div class="box-content">
					<form id="myform" class="form-horizontal" method="post" action="<c:url value="/seller/food" />">
						<fieldset>
							<input type="hidden" name="foodId" value="${food.foodId }">
							
							<div class="control-group">
								<label class="control-label" for="sellerId">商家名称</label>
								<div class="controls">
									<select id="sellerId" class="input-medium" name="sellerId"
									check-type="required" required-message="请选择所属商家">
									<c:forEach var="seller" items="${sellers }">
										<option value="${seller.sellerId }"
											<c:if test="${food.sellerId == seller.sellerId}">selected</c:if>>
											${seller.sellerName}</option>
									</c:forEach>
								</select>
								</div>
							</div>
							
							<div class="control-group">
								<label class="control-label" for="menuId">菜单分类</label>
								<div class="controls">
									<select data-placeholder="全部" id="menus_0"
									name="menuId" data-rel="chosen" class="input-medium">

									</select>
								</div>
							</div>
							
							<div class="control-group">
								<label class="control-label" for="foodName">菜品名称</label>
								<div class="controls">
									<input class="input-xlarge" id="foodName" name="foodName" type="text"
										value="${food.foodName }" maxlength="50" check-type="required" required-message="请输入菜品名称">
								</div>
							</div>
							
							<div class="control-group">
								<label class="control-label" for="price">价格</label>
								<div class="controls">
									<input class="input-xlarge" id="price" name="price" type="text"
										value="${food.price }" maxlength="30" >
								</div>
							</div>
							<div class="control-group">
								<label class="control-label" for="rebate">折扣</label>
								<div class="controls">
									<input class="input-xlarge" id="rebate" name="rebate" type="text"
										value="${food.rebate }" maxlength="30" >折
								</div>
							</div>
							<div class="control-group">
								<label class="control-label" for="initMonthSales">月销量初始值</label>
								<div class="controls">
									<input class="input-xlarge" id="initMonthSales" name="initMonthSales" type="text"
										value="${food.initMonthSales }" maxlength="10" >
								</div>
							</div>
							<div class="control-group">
								<label class="control-label" for="fileInput">上传照片</label>
								<div class="controls">
									<input class="input-file uniform_on" name="file"
										id="userHeader" type="file"> <input type="button"
										value="upload" onclick="return ajaxFileUpload();">
								</div>
								<img id="loading" class="hide" src="<c:url value="/resources/img/ajax-loader-6.gif"/>">
								<input type="hidden" name="pic" id="pic" value="${food.pic }">
							</div>
							<div class="control-group">
								
								<div class="controls">
									<img id="picture" src='${food.pic }' class="input-xxlarge">
								</div>
								
							</div>
							
							<div class="form-actions">
								<button id="saveBtn" type="button" class="btn btn-primary">保存</button>
								<button id="cancelBtn" type="button" class="btn">返回</button>
							</div>
						</fieldset>
					</form>

				</div>
			</div>
			<!--/span-->

		</div>
	</div>
	<!--/row-->
</body>

<jsp:include page="frame/scripts.jsp"></jsp:include>
<script type="text/javascript">
$(document).ready(function(){
	
	$('#cancelBtn').click(function(){
		window.location.href = '<c:url value="/seller/food" />';	
	})
	
	$("#myform").validation();
	
	$('#sellerId').change(function(){
		var sellerId = $(this).val();
		$.ajax({
			url:'<c:url value="/seller/menu/tree" />',
			dataType:'json',
			data:{sellerId:sellerId},
			success:function(data){
				$("#menus_0").html('');
				//搜索表单初始化版块下拉框
				createTree(data,"menus");
				//设置选中
				$("#menus_0").val("${food.menuId}");
				
				
			}
		})
	}).trigger('change');
	
	//保存
	$('#saveBtn').click(function(){
		$("#myform").submit();
	})
})

//上传图片
function ajaxFileUpload(){
	
	$("#loading").ajaxStart(function(){
		//开始上传文件时显示一个图片
		$(this).show();
	}).ajaxComplete(function(){
		//文件上传完成将图片隐藏起来
		$(this).hide();
	});
	
	$.ajaxFileUpload({
		url:'<c:url value="/fileupload"/>',//用于文件上传的服务器端请求地址
		secureuri:false,//一般设置为false
		fileElementId:'userHeader',//文件上传空间的id属性  <input type="file" id="file" name="file" />
		dataType: 'JSON',//返回值类型 一般设置为json(大写)
		success: function (data, status){
			var image = DOMAIN + '/resources/fileupload/'+$(data).html();
			$("#picture").attr('src',image).show();
			$("input[name='pic']").val(image);
		},
		error: function (data, status, e){
			alert(e);
		}	
	})
	
	return false;
}

function createTree(data,tagId){
	$.each(data,function(i,item){
		//生成节点
		createNode(item,tagId);
		if(item.children.length > 0){
			//生成子节点
			createTree(item.children,tagId);
		}
	})
}

function createNode(item,tagId){
	var $parent_select = $("#"+tagId+"_"+item.menuParent);
	var str = '';
	if(item.children.length > 0){
		str += '<optgroup id="'+tagId+'_'+item.menuId+'" label="'+item.menuName+'">';
		str += '</optgroup>';
	}else{
		str += '<option value='+item.menuId+'>'+item.menuName+'</option>';
	}
	
	$parent_select.append(str);
}
</script>